package com.example.mailsender.controller;

import com.example.mailsender.Service.MailService;
import com.example.mailsender.dao.MailDao;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@RestController
@Api("邮件服务")
public class MailController {
    @Autowired
    MailService mailService;


    @ApiOperation("发送不带附件的文件")
    @PostMapping("/send")
    public String sendMail(@RequestBody MailDao mailDao) throws MessagingException {

        String res = mailService.sendMailHtml(mailDao);
        return res.equals("ok")?"邮件发送成功":"邮件发送失败";
    }
    @ApiOperation("发送携带附件的文件")
    @PostMapping("/sendwithOther")
    public String sendMailwithOther(MultipartFile file, MailDao mailDao) throws MessagingException, IOException {
        String res;
        try {
            InputStream bb = file.getInputStream();
            System.out.println(file.getOriginalFilename());
            InputStreamResource inputStreamResource = new InputStreamResource(bb,file.getOriginalFilename());
            res = mailService.sendMailHtmlwithOther(mailDao,inputStreamResource,file.getOriginalFilename());
        }catch (Exception e){

            return "邮件发送失败";
        }
        return res.equals("ok")?"邮件发送成功":"邮件发送失败";
    }
}
